﻿Imports System.Data
Imports MyGeneration.dOOdads

Public Class rc_Servicios

    Private _MessageError As Exception = Nothing
    Public Property MessageError() As Exception
        Get
            Return _MessageError
        End Get
        Set(value As Exception)
            _MessageError = value
        End Set
    End Property


#Region "Servicios"

    Public Function Insert_Servicios(conn As String, descripcion As String, costo As Decimal, codTipoServicio As Integer,
                                     precio As Decimal, codUsuario As Integer) As Integer

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim servicio As New Class_RC_Data.Class_RC.SERVICIOS(conn)

            servicio.AddNew()
            servicio.Descripcion = descripcion
            servicio.Costo = costo
            servicio.Codtiposervicio = codTipoServicio
            servicio.Precio = precio
            servicio.CodUsuario = codUsuario
            servicio.FechaActualizacion = DateTime.Now
            servicio.Save()

            tx.CommitTransaction()

            Return servicio.Codservicio

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return 0
        End Try

    End Function

    Public Function Update_Servicios(conn As String, codServicio As Integer,
                                     descripcion As String, costo As Decimal, codTipoServicio As Integer,
                                     precio As Decimal, codUsuario As Integer) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim servicio As New Class_RC_Data.Class_RC.SERVICIOS(conn)
            servicio.Where.Codservicio.Value = codServicio

            If servicio.Query.Load Then

                servicio.Descripcion = descripcion
                servicio.Costo = costo
                servicio.Codtiposervicio = codTipoServicio
                servicio.Precio = precio
                servicio.CodUsuario = codUsuario
                servicio.FechaActualizacion = DateTime.Now
                servicio.Save()

            End If

            tx.CommitTransaction()
            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function Delete_Servicios(conn As String, codServicio As Integer) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim servicio As New Class_RC_Data.Class_RC.SERVICIOS(conn)
            servicio.Where.Codservicio.Value = codServicio

            If servicio.Query.Load Then

                servicio.MarkAsDeleted()
                servicio.Save()

            End If

            tx.CommitTransaction()
            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function get_Servicios_All(conn As String, codServicio As Integer, descripcion As String,
                                      codTipoServicio As Integer, soloCosto As Boolean) As DataTable
        Try

            Dim vServicios As New Class_RC_Data.Class_RC.VistaServicios(conn)

            If codServicio > 0 Then
                vServicios.Where.Codservicio.Value = codServicio
            End If

            If descripcion <> "" Then
                vServicios.Where.Descripcion.Value = "%" & descripcion & "%"
                vServicios.Where.Descripcion.Operator = WhereParameter.Operand.Like
            End If

            If codTipoServicio > 0 Then
                vServicios.Where.Codtiposervicio.Value = codTipoServicio
            End If

            If soloCosto Then
                vServicios.Where.Costo.Value = 0
                vServicios.Where.Costo.Operator = WhereParameter.Operand.GreaterThan
            End If

            If vServicios.Query.Load Then
                If vServicios.DefaultView.Table.Rows.Count > 0 Then
                    Return vServicios.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

    Public Function get_tipo_Servicios(conn As String) As DataTable
        Try

            Dim vTipoServicios As New Class_RC_Data.Class_RC.TIPOSERVICIO(conn)

            If vTipoServicios.Query.Load Then
                If vTipoServicios.DefaultView.Table.Rows.Count > 0 Then
                    Return vTipoServicios.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

#End Region

End Class





